package com.jieshuibao.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.jieshuibao.mapper.AdvisoryMapper;
import com.jieshuibao.mapper.AdvisoryRecordingExMapper;
import com.jieshuibao.mapper.AdvisoryRecordingMapper;
import com.jieshuibao.mapper.AdvisoryUserMapper;
import com.jieshuibao.mapper.CounselorMapper;
import com.jieshuibao.mapper.UserDealRecordMapper;
import com.jieshuibao.mapper.UserExMapper;
import com.jieshuibao.mapper.UserMapper;
import com.jieshuibao.model.db.Advisory;
import com.jieshuibao.model.db.AdvisoryExample;
import com.jieshuibao.model.db.AdvisoryRecording;
import com.jieshuibao.model.db.AdvisoryUser;
import com.jieshuibao.model.db.AdvisoryUserExample;
import com.jieshuibao.model.db.Counselor;
import com.jieshuibao.model.db.UserDealRecord;
import com.jieshuibao.model.db.AdvisoryExample.Criteria;
import com.jieshuibao.model.db.AdvisoryRecordingExample;
import com.jieshuibao.model.db.User;
import com.jieshuibao.util.JsonUtil;
import com.jieshuibao.util.MoneyRandom;
import com.jieshuibao.util.PayTypeUtil;
import com.jieshuibao.util.UUIDGenerator;
import com.jieshuibao.wx.util.PayCommonUtil;


@Service
public class AdvistoryTimeService {
	
	private static final Logger logger = Logger.getLogger(AdvisoryRecordingService.class);
	
	@Resource
	private AdvisoryMapper advisoryMapper;
	
	@Resource
	private AdvisoryRecordingMapper advisoryRecordingMapper;
	
	@Resource
	private UserMapper userMapper;
	
	@Resource
	private UserExMapper userExMapper;
	
	@Resource
	private CounselorMapper counselorMapper;
	
	@Resource
	private UserDealRecordMapper userDealRecordMapper; 
	
	@Resource
	private AdvisoryUserMapper advisoryUserMapper;
	
	@Resource
	private RedisService redisService;
	
	@Resource
	private AdvisoryRecordingExMapper advisoryRecordingExMapper;
	
	
	//咨询师未回应
	public void updateAdvisoryNoAnwer(){
		
		AdvisoryExample advisoryExample = new AdvisoryExample();
		
		Criteria createCriteria = advisoryExample.createCriteria();
		createCriteria.andTypeEqualTo("0");
		List<Advisory> list = advisoryMapper.selectByExample(advisoryExample);
		if(list.size()>0){
			
			for (Advisory advisory : list) {
				String uuid = advisory.getUuid();
				String userRes = advisory.getUserRes();
				Long startTime = advisory.getStartTime();
				Integer money = advisory.getMoney();
				String userAsk = advisory.getUserAsk();
				String onlyId = advisory.getOnlyId();
				
				AdvisoryRecordingExample advisoryRecordingExample = new AdvisoryRecordingExample();
				com.jieshuibao.model.db.AdvisoryRecordingExample.Criteria createCriteria2 = advisoryRecordingExample.createCriteria();
				createCriteria2.andAdvisoryIdEqualTo(uuid);
				createCriteria2.andUserIdEqualTo(userRes);
				int i = advisoryRecordingMapper.countByExample(advisoryRecordingExample);
				if(i==0){
					//咨询师没反应
					long currentTimeMillis = System.currentTimeMillis();
					long endTime=startTime+6*60*60*1000;
					if(endTime<=currentTimeMillis){
						//结束业务 给用户退钱 记流水 改评价 聊天记录结束
						Advisory advisory2 = new Advisory();
						advisory2.setUuid(uuid);
						advisory2.setEndTime(currentTimeMillis);
						advisory2.setType("3");
						User user = userMapper.selectByPrimaryKey(userAsk);
						Counselor counselor = userExMapper.selectCounseleorByPhone(userRes);
						
						
						if(user!=null){
							Double balance = user.getBalance();
							User user2 = new User();
							user2.setPhoneNumber(userAsk);
							user2.setBalance(balance+money);
							String uuid1 = UUIDGenerator.getUUID();
							String uuid2 = UUIDGenerator.getUUID();
							UserDealRecord userDealRecord1=new UserDealRecord();
							UserDealRecord userDealRecord2=new UserDealRecord();
							userDealRecord1.setUuid(uuid1);//用户收入
							userDealRecord2.setUuid(uuid2);//解税宝公司支出
							
							userDealRecord1.setAccountType(PayTypeUtil.ACCOUNTTYPE_PERSONAL);
							userDealRecord2.setAccountType(PayTypeUtil.ACCOUNTTYPE_BUSINESS);
							
							userDealRecord2.setUserId(PayTypeUtil.COMPANY_ACCPUNT);
							userDealRecord1.setUserId(userAsk);
							
							Date date = new Date();
							userDealRecord1.setBusinessNo(uuid);
							userDealRecord2.setBusinessNo(uuid);
							userDealRecord1.setCreateDate(date);
							userDealRecord2.setCreateDate(date);
							userDealRecord1.setSum((double)money);
							userDealRecord2.setSum((double)money);
							
							userDealRecord1.setGoodsType(PayTypeUtil.GOODSTYPE_ADVISORYBACK);
							userDealRecord2.setGoodsType(PayTypeUtil.GOODSTYPE_ADVISORYBACK);
							
							userDealRecord1.setTradeType(PayTypeUtil.TRDETYPE_INCOME);
							userDealRecord2.setTradeType(PayTypeUtil.TRDETYPE_EXPENDITURE);
							userDealRecord1.setPaySucTime(date);
							userDealRecord2.setPaySucTime(date);
							
							userDealRecord1.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
							userDealRecord2.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
							
							userDealRecord1.setPayType(PayTypeUtil.PAYTYPE_BALANCE);
							userDealRecord2.setPayType(PayTypeUtil.PAYTYPE_COMPANYENVELOPES);
							
							userMapper.updateByPrimaryKeySelective(user2);
							logger.info("一对一咨询  咨询师未回应给用户退钱："+JsonUtil.toJson(user2));
							userDealRecordMapper.insertSelective(userDealRecord1);
							logger.info("一对一咨询 咨询师未回应给用户退钱流水："+JsonUtil.toJson(userDealRecord1));
							userDealRecordMapper.insertSelective(userDealRecord2);
							logger.info("一对一咨询 咨询师未回应给用户退钱流水解税宝："+JsonUtil.toJson(userDealRecord2));
							//聊天结束
							AdvisoryRecording advisoryRecording2 = new AdvisoryRecording();
							String uuid3 = UUIDGenerator.getUUID();
							advisoryRecording2.setUuid(uuid3);
							advisoryRecording2.setAdvisoryId(uuid);
							advisoryRecording2.setOnlyId(onlyId);
							advisoryRecording2.setUserId(userAsk);
							advisoryRecording2.setChatTime(currentTimeMillis);
							advisoryRecording2.setType("4");
							advisoryRecording2.setContent("#@@^&@@#");
							advisoryRecordingMapper.updateByPrimaryKeySelective(advisoryRecording2);
							
							AdvisoryUserExample advisoryUserExample1 = new AdvisoryUserExample();
							com.jieshuibao.model.db.AdvisoryUserExample.Criteria createCriteria3 = advisoryUserExample1.createCriteria();
							createCriteria3.andOnlyIdEqualTo(onlyId);
							createCriteria3.andUserOneEqualTo(userAsk);
							AdvisoryUser advisoryUser1 = new AdvisoryUser();
							advisoryUser1.setLastTime(currentTimeMillis);
							advisoryUser1.setMessage("#@@^&@@#");
							advisoryUser1.setType("0");
							advisoryUserMapper.updateByExampleSelective(advisoryUser1, advisoryUserExample1);
							AdvisoryUserExample advisoryUserExample2 = new AdvisoryUserExample();
							com.jieshuibao.model.db.AdvisoryUserExample.Criteria createCriteria4 = advisoryUserExample2.createCriteria();
							createCriteria4.andOnlyIdEqualTo(onlyId);
							createCriteria4.andUserOneEqualTo(userRes);
							AdvisoryUser advisoryUser2 = new AdvisoryUser();
							advisoryUser2.setLastTime(currentTimeMillis);
							advisoryUser2.setMessage("#@@^&@@#");
							advisoryUser2.setType("1");
							advisoryUserMapper.updateByExampleSelective(advisoryUser2, advisoryUserExample2);
							
							if(counselor!=null){
								Integer advNum = counselor.getAdvNum();
								Integer advScore = counselor.getAdvScore();
								counselor.setAdvNum(advNum-1);
								counselor.setAdvScore(advScore-5);
								counselorMapper.updateByPrimaryKeySelective(counselor);
							}
						}
					}
				}
			}
		}
	}
	
	
	
	
	//咨询师点结束 用户到期没有操作
	public void updateAdvisoryUserExpired(){
		AdvisoryExample advisoryExample = new AdvisoryExample();
		Criteria createCriteria = advisoryExample.createCriteria();
		createCriteria.andTypeEqualTo("2");
		// List<Advisory> list =
		// advisoryService.getAdvisoryByExample(advisoryExample);
		List<Advisory> list = advisoryMapper.selectByExample(advisoryExample);
		for (Advisory advisory : list) {

			String uuid = advisory.getUuid();
			String onlyId = advisory.getOnlyId();
			String userRes = advisory.getUserRes();
			Integer money = advisory.getMoney();
			
			String string = redisService.get(uuid + "_end");
			// AdvisoryRecording advisoryRecording =
			// advisoryRecordingService.getAdvisoryRecordingByUuid(string);
			AdvisoryRecording advisoryRecording = advisoryRecordingMapper
					.selectByPrimaryKey(string);
			
			User user = userMapper.selectByPrimaryKey(userRes);
			
			if (advisoryRecording != null&&user!=null) {

				Long chatTime = advisoryRecording.getChatTime();
				long endTime = chatTime + 20 * 60 * 1000;
				long currentTimeMillis = System.currentTimeMillis();
				double advisorySubMoney = MoneyRandom.getAdvisorySubMoney(money);
				if (endTime < currentTimeMillis) {
					// 结束业务
					Advisory advisory2 = new Advisory();
					advisory2.setEndTime(currentTimeMillis);
					advisory2.setUuid(uuid);
					advisory2.setType("3");

					AdvisoryUserExample advisoryUserExample = new AdvisoryUserExample();
					com.jieshuibao.model.db.AdvisoryUserExample.Criteria createCriteria2 = advisoryUserExample
							.createCriteria();
					createCriteria2.andOnlyIdEqualTo(onlyId);
					// List<AdvisoryUser> list1 =
					// advisoryUserService.getAdvisoryUserByExample(advisoryUserExample);
					List<AdvisoryUser> list1 = advisoryUserMapper
							.selectByExample(advisoryUserExample);
					AdvisoryUser advisoryUser1 = null;
					AdvisoryUser advisoryUser2 = null;
					advisoryUser1 = list1.get(0);
					advisoryUser2 = list1.get(1);
					String userOne = advisoryUser1.getUserOne();
					advisoryUser1.setLastTime(currentTimeMillis);
					advisoryUser2.setLastTime(currentTimeMillis);
					if (userOne.equals(userRes)) {
						advisoryUser1.setType("0");
						advisoryUser2.setType("1");
					} else {
						advisoryUser1.setType("1");
						advisoryUser2.setType("0");
					}
					advisoryUser1.setMessage("#@@&@@#");
					advisoryUser2.setMessage("#@@&@@#");

					AdvisoryRecording advisoryRecording2 = new AdvisoryRecording();
					String uuid3 = UUIDGenerator.getUUID();
					advisoryRecording2.setUuid(uuid3);
					advisoryRecording2.setAdvisoryId(uuid);
					advisoryRecording2.setOnlyId(onlyId);
					advisoryRecording2.setUserId(userRes);
					advisoryRecording2.setChatTime(currentTimeMillis);
					advisoryRecording2.setType("4");
					advisoryRecording2.setContent("#@@&@@#");
					
					//记流水
					String uuid1 = UUIDGenerator.getUUID();
					String uuid2 = UUIDGenerator.getUUID();
					UserDealRecord userDealRecord1=new UserDealRecord();
					UserDealRecord userDealRecord2=new UserDealRecord();
					userDealRecord1.setUuid(uuid1);//用户收入
					userDealRecord2.setUuid(uuid2);//解税宝公司支出
					
					userDealRecord1.setAccountType(PayTypeUtil.ACCOUNTTYPE_PERSONAL);
					userDealRecord2.setAccountType(PayTypeUtil.ACCOUNTTYPE_BUSINESS);
					
					userDealRecord2.setUserId(PayTypeUtil.COMPANY_ACCPUNT);
					userDealRecord1.setUserId(userRes);
					
					Date date = new Date();
					userDealRecord1.setBusinessNo(uuid);
					userDealRecord2.setBusinessNo(uuid);
					userDealRecord1.setCreateDate(date);
					userDealRecord2.setCreateDate(date);
					userDealRecord1.setSum(advisorySubMoney);
					userDealRecord2.setSum(advisorySubMoney);
					
					userDealRecord1.setGoodsType(PayTypeUtil.GOODSTYPE_ADVISORYBACK);
					userDealRecord2.setGoodsType(PayTypeUtil.GOODSTYPE_ADVISORYBACK);
					
					userDealRecord1.setTradeType(PayTypeUtil.TRDETYPE_INCOME);
					userDealRecord2.setTradeType(PayTypeUtil.TRDETYPE_EXPENDITURE);
					userDealRecord1.setPaySucTime(date);
					userDealRecord2.setPaySucTime(date);
					
					userDealRecord1.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
					userDealRecord2.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
					
					userDealRecord1.setPayType(PayTypeUtil.PAYTYPE_BALANCE);
					userDealRecord2.setPayType(PayTypeUtil.PAYTYPE_COMPANYENVELOPES);
					
					Double balance = user.getBalance();
					User user2 = new User();
					user2.setBalance(balance+advisorySubMoney);
					user2.setPhoneNumber(userRes);
					advisoryRecordingMapper.updateByPrimaryKeySelective(advisoryRecording2);
					advisoryUserMapper.updateByPrimaryKeySelective(advisoryUser1);
					advisoryUserMapper.updateByPrimaryKeySelective(advisoryUser2);
					userDealRecordMapper.insertSelective(userDealRecord1);
					logger.info("一对一咨询 到期咨询师分钱流水："+JsonUtil.toJson(userDealRecord1));
					userDealRecordMapper.insertSelective(userDealRecord2);
					logger.info("一对一咨询 到期 解税宝分钱流水："+JsonUtil.toJson(userDealRecord2));
					userMapper.updateByPrimaryKeySelective(user2);
					logger.info("一对一咨询  到期给咨询师分钱："+JsonUtil.toJson(user2));
					redisService.delete(uuid + "_end");
					redisService.delete(uuid + "_disagree");

				}
			} else {

				System.out.println("advisoryRecording是空");
			}

		}

	}
	
	//双方都没有点击结束
	
	
	public void updateAdvisoryExpired(){

		
		AdvisoryExample advisoryExample = new AdvisoryExample();
		
		Criteria createCriteria = advisoryExample.createCriteria();
		createCriteria.andTypeEqualTo("0");
		List<Advisory> list = advisoryMapper.selectByExample(advisoryExample);
		if(list.size()>0){
			
			for (Advisory advisory : list) {
				String uuid = advisory.getUuid();
				String userRes = advisory.getUserRes();
				Integer money = advisory.getMoney();
				String userAsk = advisory.getUserAsk();
				String onlyId = advisory.getOnlyId();
				
				AdvisoryRecordingExample advisoryRecordingExample = new AdvisoryRecordingExample();
				com.jieshuibao.model.db.AdvisoryRecordingExample.Criteria createCriteria2 = advisoryRecordingExample.createCriteria();
				createCriteria2.andAdvisoryIdEqualTo(uuid);
				createCriteria2.andUserIdEqualTo(userRes);
				int i = advisoryRecordingMapper.countByExample(advisoryRecordingExample);
				if(i>0){
					AdvisoryRecording advisoryRecording = advisoryRecordingExMapper.selectMaxChatTime(uuid);
					Long chatTime = advisoryRecording.getChatTime();
					long currentTimeMillis = System.currentTimeMillis();
					long endTime=chatTime+2*60*60*1000;
					if(endTime<=currentTimeMillis){
						//结束业务 给咨询师分钱  记流水  聊天记录结束
						Advisory advisory2 = new Advisory();
						advisory2.setUuid(uuid);
						advisory2.setEndTime(currentTimeMillis);
						advisory2.setType("3");
						User user = userMapper.selectByPrimaryKey(userRes);
						double advisorySubMoney = MoneyRandom.getAdvisorySubMoney(money);
						
						if(user!=null){
							Double balance = user.getBalance();
							User user2 = new User();
							user2.setPhoneNumber(userRes);
							user2.setBalance(balance+advisorySubMoney);
							String uuid1 = UUIDGenerator.getUUID();
							String uuid2 = UUIDGenerator.getUUID();
							UserDealRecord userDealRecord1=new UserDealRecord();
							UserDealRecord userDealRecord2=new UserDealRecord();
							userDealRecord1.setUuid(uuid1);//用户收入
							userDealRecord2.setUuid(uuid2);//解税宝公司支出
							
							userDealRecord1.setAccountType(PayTypeUtil.ACCOUNTTYPE_PERSONAL);
							userDealRecord2.setAccountType(PayTypeUtil.ACCOUNTTYPE_BUSINESS);
							
							userDealRecord2.setUserId(PayTypeUtil.COMPANY_ACCPUNT);
							userDealRecord1.setUserId(userRes);
							
							Date date = new Date();
							userDealRecord1.setBusinessNo(uuid);
							userDealRecord2.setBusinessNo(uuid);
							userDealRecord1.setCreateDate(date);
							userDealRecord2.setCreateDate(date);
							userDealRecord1.setSum(advisorySubMoney);
							userDealRecord2.setSum(advisorySubMoney);
							
							userDealRecord1.setGoodsType(PayTypeUtil.GOODSTYPE_ADVISORYBACK);
							userDealRecord2.setGoodsType(PayTypeUtil.GOODSTYPE_ADVISORYBACK);
							
							userDealRecord1.setTradeType(PayTypeUtil.TRDETYPE_INCOME);
							userDealRecord2.setTradeType(PayTypeUtil.TRDETYPE_EXPENDITURE);
							userDealRecord1.setPaySucTime(date);
							userDealRecord2.setPaySucTime(date);
							
							userDealRecord1.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
							userDealRecord2.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
							
							userDealRecord1.setPayType(PayTypeUtil.PAYTYPE_BALANCE);
							userDealRecord2.setPayType(PayTypeUtil.PAYTYPE_COMPANYENVELOPES);
							
							userMapper.updateByPrimaryKeySelective(user2);
							logger.info("一对一咨询  双方未点击结束给咨询师分钱："+JsonUtil.toJson(user2));
							userDealRecordMapper.insertSelective(userDealRecord1);
							logger.info("一对一咨询 双方未点击结束给咨询师分钱流水："+JsonUtil.toJson(userDealRecord1));
							userDealRecordMapper.insertSelective(userDealRecord2);
							logger.info("一对一咨询 双方未点击结束给咨询师分钱钱流水解税宝："+JsonUtil.toJson(userDealRecord2));
							//聊天结束
							AdvisoryRecording advisoryRecording2 = new AdvisoryRecording();
							String uuid3 = UUIDGenerator.getUUID();
							advisoryRecording2.setUuid(uuid3);
							advisoryRecording2.setAdvisoryId(uuid);
							advisoryRecording2.setOnlyId(onlyId);
							advisoryRecording2.setUserId(userAsk);
							advisoryRecording2.setChatTime(currentTimeMillis);
							advisoryRecording2.setType("4");
							advisoryRecording2.setContent("#@@^&@@#");
							advisoryRecordingMapper.updateByPrimaryKeySelective(advisoryRecording2);
							
							AdvisoryUserExample advisoryUserExample1 = new AdvisoryUserExample();
							com.jieshuibao.model.db.AdvisoryUserExample.Criteria createCriteria3 = advisoryUserExample1.createCriteria();
							createCriteria3.andOnlyIdEqualTo(onlyId);
							createCriteria3.andUserOneEqualTo(userAsk);
							AdvisoryUser advisoryUser1 = new AdvisoryUser();
							advisoryUser1.setLastTime(currentTimeMillis);
							advisoryUser1.setMessage("#@@^&@@#");
							advisoryUser1.setType("0");
							advisoryUserMapper.updateByExampleSelective(advisoryUser1, advisoryUserExample1);
							AdvisoryUserExample advisoryUserExample2 = new AdvisoryUserExample();
							com.jieshuibao.model.db.AdvisoryUserExample.Criteria createCriteria4 = advisoryUserExample2.createCriteria();
							createCriteria4.andOnlyIdEqualTo(onlyId);
							createCriteria4.andUserOneEqualTo(userRes);
							AdvisoryUser advisoryUser2 = new AdvisoryUser();
							advisoryUser2.setLastTime(currentTimeMillis);
							advisoryUser2.setMessage("#@@^&@@#");
							advisoryUser2.setType("1");
							advisoryUserMapper.updateByExampleSelective(advisoryUser2, advisoryUserExample2);
						}
					}
				}
			}
		}
	}
}
